package com.jay.lang.intf;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class CloneIntf {
	public static void main(String[] args) {
		Teacher t = new Teacher("Name", 22);
		Teacher m = (Teacher) t.clone();
		System.out.println(m);

		Student stu = new Student();
		Student deepcloneSTU = (Student) stu.clone();
		stu.courses.put(new Integer(1), "Math");
		deepcloneSTU.courses.put(new Integer(100), "Java");
		disp(stu.courses);
		disp(deepcloneSTU.courses);

	}

	static void disp(HashMap<?, ?> h) {
		Set<?> keySet = h.keySet();
		Iterator<?> it = keySet.iterator();
		while (it.hasNext()) {
			System.out.println(h.get(it.next()));
		}
	}
}

class Teacher implements Cloneable {
	String name;
	int age;

	Teacher(String name, int age) {
		this.name = name;
		this.age = age;
	}

	public Object clone() {
		try {
			return super.clone();
		} catch (CloneNotSupportedException e) {
			throw new Error("This should never happen!");
		}
	}

	public String toString() {
		return name + "  " + age;
	}
}

class Student implements Cloneable {
	HashMap<Integer, String> courses = new HashMap<Integer, String>();

	Student() {
	}

	@SuppressWarnings("unchecked")
	public Object clone() {
		try {
			Student stu = (Student) super.clone();
			stu.courses = (HashMap<Integer, String>) courses.clone();
			return stu;
		} catch (CloneNotSupportedException e) {
			throw new Error("This should never happen!");
		}
	}

}
